package com.example.filesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.filesystem.entity.Sequence;
import com.example.filesystem.mapper.SequenceMapper;
import com.example.filesystem.service.ISequenceService;
import org.springframework.stereotype.Service;

/**
 * @author bbyh
 * @since 2025-05-02
 */
@Service
public class SequenceServiceImpl extends ServiceImpl<SequenceMapper, Sequence> implements ISequenceService {
    @Override
    public Integer nextDirSeqVal(Integer userId) {
        LambdaQueryWrapper<Sequence> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Sequence::getUserId, userId).eq(Sequence::getSequenceName, "dir");
        Sequence sequence = getOne(wrapper);
        if (sequence == null) {
            sequence = new Sequence();
            sequence.setUserId(userId);
            sequence.setSequenceName("dir");
            sequence.setSequenceValue(1);
            save(sequence);
            return 1;
        }

        sequence.setSequenceValue(sequence.getSequenceValue() + 1);
        update(sequence, wrapper);
        return sequence.getSequenceValue();
    }
}
